package com.adtech.core.util;

import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.struts2.views.freemarker.StrutsClassTemplateLoader;

import com.adtech.core.Settings;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * FreeMarker模板处理工具
 *
 * @since 2011-11-2
 * @author Dormin
 *
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class TemplateUtil {
	
	private static final String ENCODING = "UTF-8";

	private static Map<String,Template> templateCache = new ConcurrentHashMap<String,Template>();

	/**
	 * 根据名称查找模板
	 * @param templateName 模板名称
	 * @param cache 是否缓存模板
	 *
	 * @return FreeMarker模板
	 * @throws IOException
	 */
	private static Template getTemplate(String templateName, boolean cache) throws IOException {
		Template template = templateCache.get(templateName);
		if(template==null){
//			String path = FileUtil.getFilePath(templateName);
//			String name = FileUtil.getFileName(templateName);
			// 获取模板
			StrutsClassTemplateLoader loader = new StrutsClassTemplateLoader();
			Configuration config = new Configuration();
			config.setTemplateLoader(loader);
			config.setDefaultEncoding(ENCODING);
//			config.setDirectoryForTemplateLoading(new File(path));
			template = config.getTemplate(templateName, ENCODING);
		}

		//缓存模板
		if (cache){
			templateCache.put(templateName, template);
		}

		return template;
	}

	/**
	 * 用数据填充模板并返回渲染结果
	 * @param templateName 模板名称
	 * @param rootMap 数据
	 * @param cache 是否缓存模板
	 *
	 * @return 渲染结果
	 * @throws TemplateException
	 * @throws IOException 
	 *
	 * @author Dormin
	 */
	public static String process(String templateName, Map<String, ? extends Object> rootMap, boolean cache) throws TemplateException, IOException {
		StringWriter writer = new StringWriter();
		StringBuffer buffer = new StringBuffer();
		Template template = getTemplate(templateName, cache);
		if(template!=null){
			template.setEncoding(ENCODING);
			template.process(rootMap, writer);
			writer.flush();
			buffer = writer.getBuffer();
		}

		return buffer.toString();
	}
	
	/**
	 * 用数据填充模板并返回渲染结果
	 * @param templateName 模板名称
	 * @param templateReader 模板内容流
	 * @param rootMap 数据
	 *
	 * @return 渲染结果
	 * @throws TemplateException
	 * @throws IOException 
	 *
	 * @author Dormin
	 */
	public static String process(String templateName,Reader templateReader, Map<String, ? extends Object> rootMap) throws TemplateException, IOException {
		StringWriter writer = new StringWriter();
		StringBuffer buffer = new StringBuffer();
		Configuration config = new Configuration();
		config.setDefaultEncoding(ENCODING);
		Template template = new Template(templateName, templateReader,config);
		if(template!=null){
			template.setEncoding(ENCODING);
			template.process(rootMap, writer);
			writer.flush();
			buffer = writer.getBuffer();
		}
		return buffer.toString();
	}

	
	/**
	 * 用数据填充模板并返回渲染结果（不缓存模板）
	 * @param templateName 模板名称
	 * @param rootMap 数据
	 *
	 * @return 渲染结果
	 * @throws TemplateException
	 * @throws IOException 
	 *
	 * @author Dormin
	 */
	public static String process(String templateName, Map<String, ? extends Object> rootMap) throws TemplateException, IOException {
		return process(templateName, rootMap, Settings.CACHE);
	}

}
